package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Charge;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/charge")
public class ChargeController {

  @Autowired ChargeService chargeService;

  // 获取收费列表
  @GetMapping("/chargeList")
  public Response getChargeList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Charge> chargeList = chargeService.getChargeList(page, count);
      return ResponseUtils.buildSuccess(chargeList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchCharge")
  public Response searchCharge(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "chargeType", defaultValue = "null") String chargeType,
      @RequestParam(value = "chargeName", defaultValue = "null") String chargeName) {
    try {
      PageInfo<Charge> searchList = chargeService.searchCharge(page, count, chargeType, chargeName);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 增加收费规则
  @PostMapping("/addCharge")
  public Response addCompany(@RequestBody Charge charge) {
    Boolean addCharge = chargeService.addCharge(charge);
    if (addCharge) {
      return ResponseUtils.buildSuccess("增加计费规则成功");
    }
    return ResponseUtils.buildError("增加计费规则失败");
  }

  // 修改收费规则
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody Charge charge) {
    Boolean changeInfo = chargeService.chargeInfo(charge);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改计费规则成功");
    }
    return ResponseUtils.buildError("修改计费规则失败");
  }

  // 删除收费规则
  @PostMapping("/deleteCharge")
  public Response deleteCharge(@RequestParam("chargeId") Integer chargeId) {
    Boolean delete = chargeService.deleteCharge(chargeId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除计费规则成功");
    }
    return ResponseUtils.buildError("删除计费规则失败");
  }
}
